//
// Created by bleedingfight on 2020/7/4.
//

#include "maxValue.h"
int maxValue(std::vector<std::vector<int>>& grid) {
  int rows = grid.size();
  int cols = grid[0].size();
  int dp[rows][cols];
  for (int r = 0; r < rows; r++) {
    for (int c = 0; c < cols; c++) {
      if (r == 0 && c == 0)
        dp[r][c] = grid[r][c];
      else if (r == 0 && c != 0)
        dp[r][c] = dp[r][c - 1] + grid[r][c];
      else if (c == 0 && r != 0)
        dp[r][c] = dp[r - 1][c] + grid[r][c];
      else
        dp[r][c] = dp[r - 1][c] > dp[r][c - 1] ? dp[r - 1][c] + grid[r][c]
                                               : dp[r][c - 1] + grid[r][c];
    }
  }
  return dp[rows - 1][cols - 1];
}
